package com.gljx.web.controller.JournalController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gljx.web.Entity.Person;
import com.gljx.web.Entity.TrainCharge;
import com.gljx.web.Entity.User;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.MakeupChargeService;
import com.gljx.web.service.OtherPayDemandService;
import com.gljx.web.service.OwnCarTrainService;
import com.gljx.web.service.PersonService;
import com.gljx.web.service.TrainChargeService;
import com.gljx.web.service.UserService;

@Controller
@RequestMapping("/")
public class CashDayJournalController {
	
	@Autowired
	private TrainChargeService trainChargeService;
	
	@Autowired
	private UserService userService;
	
	
	
	/**
	 * 收银日报表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/selectCashDayJournal")
	public String selectCashDayJournal(HttpServletRequest request){
		// 拼接SQL语句
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");   
		List<User> users = userService.queryUserTypeByCompanyId(user.getFcompanyid());
		Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
				 cale.add(Calendar.DATE, -1);		 		
		Date resultDate = cale.getTime(); // 结果
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String beginTime = sdf.format(resultDate);
		String endTime = sdf.format(new Date());  
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("users", users);
		return "backHome/journaling/financialJournaling/cashDayJournaling/cashDayJournal";	
	}
	
	/**
	 * 根据条件查询收银日报表
	 * @param request
	 * @param beginTime
	 * @param endTime
	 * @param charge
	 * @param makeUpCharge
	 * @param examCharge
	 * @param carCharge
	 * @param trainCharge
	 * @return
	 */
	@RequestMapping(value="/selectCashDayJournalByCon")
	public String selectCashDayJournalByCon(HttpServletRequest request,String beginTime,String endTime
			,String charge ,String makeUpCharge,String examCharge,String carCharge,String trainCharge,String cfdepname,String cfdepeid){
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");     
		List<User> users = userService.queryUserTypeByCompanyId(user.getFcompanyid());
		Map<String , String> map = new HashMap<>();
							 map.put("beginTime", beginTime);
							 map.put("endTime", endTime);
							 map.put("charge", charge);
							 map.put("makeUpCharge", makeUpCharge);
							 map.put("examCharge", examCharge);
							 map.put("carCharge", carCharge);
							 map.put("trainCharge", trainCharge);
							 map.put("cfdepeid", cfdepeid);
							 map.put("companyId", user.getFcompanyid());
		List<TrainCharge> charges = trainChargeService.selectTrainCharge(map);
		TrainCharge charge2 = trainChargeService.selectTrainChargePrice(map);
		request.setAttribute("charge2", charge2);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("charges", charges);
		request.setAttribute("makeUpCharge", makeUpCharge);
		request.setAttribute("charge", charge);
		request.setAttribute("users", users );
		request.setAttribute("carCharge", carCharge);
		request.setAttribute("trainCharge", trainCharge);
		request.setAttribute("examCharge", examCharge);
		request.setAttribute("cfdepname", cfdepname);
		request.setAttribute("cfdepeid", cfdepeid);
		return "backHome/journaling/financialJournaling/cashDayJournaling/cashDayJournal";	
	}
	
	@ResponseBody
	@RequestMapping(value="/selectUserByNameForJournal")
	public List<User> selectUserByNameForJournal(String name,HttpServletRequest request){
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");   
		Map<String, Object> map = new HashMap<>();
							map.put("companyId",user.getFcompanyid());
							map.put("name", name);
		List<User> users = userService.selectUserByNameForJournal(map);
		return users;
	}
	
	
}
